package com.taobao.chenbao.template.biz;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.taobao.chenbao.template.entity.Student;
import com.taobao.chenbao.template.entity.Teacher;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;


@Component
public class ImportBiz {

    public ExcelImportResult<T> getDatas(MultipartFile file, Class<T> tClass) throws Exception {
        ImportParams params = new ImportParams();
        params.setNeedVerfiy(true);
        params.setHeadRows(1);
        ExcelImportResult<T> result = ExcelImportUtil.importExcelMore(file.getInputStream(), tClass, params);
        return result;
    }


    public ExcelImportResult<T> getDatas(File file, Class T) throws Exception {
        ImportParams params = new ImportParams();
        params.setNeedVerfiy(true);
        params.setHeadRows(1);
        ExcelImportResult<T> result = ExcelImportUtil.importExcelMore(new FileInputStream(file), T, params);
        return result;
    }

    /**
     * @return
     * @Author jerry
     * @Description:返回文件路径
     * @time 6:20 下午
     **/
    public String export(List sources, Class<T> tClass) throws Exception {
        ExportParams exportParams = new ExportParams("批量导出", "结果");
        exportParams.setType(ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, tClass, sources);
        File file = File.createTempFile(System.currentTimeMillis() + "", ".xlsx");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        return file.getAbsolutePath();
    }
}
